import Vue from 'vue'
import Vuex from 'vuex'

import storage from '@/utils/storage'

Vue.use(Vuex)

// 定义一个refresh_token key常量
const TOKEN_KEY = 'user_token'

export default new Vuex.Store({
  state: {
    user: storage.getToken(TOKEN_KEY), //用户登录的信息
    cachesPage: ['layout'] //缓存的数据
  },
  mutations: {
    setUser(state, data) {
      state.user = data
      storage.setToken(TOKEN_KEY, state.user);
    },
    // 添加缓存
    addCachePage(state, cachesPage) {
      if (!state.cachesPage.includes(cachesPage)) {
        state.cachesPage.push(cachesPage)
      }
    },
    //清除缓存
    removeCachePage(state, cachesPage) {
      const index = state.cachesPage.indexOf(cachesPage)
      if (index !== -1) {
        state.cachesPage.splice(index, 1)
      }
    }
  },
  actions: {},
  modules: {}
})
